import 'package:easy_refresh/easy_refresh.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:taonile/config/config.dart';

import '../entity/asset_general_detail_entity.dart';
import '../entity/user_info_entity.dart';
import '../http/api.dart';
import '../provider/userinfo_provider.dart';
import '../routers/routers.dart';
import '../views/default_views.dart';
import '../views/dialog_manager.dart';

///
/// 总资产包
///
class AssetPackgeDetail extends StatefulWidget {
  const AssetPackgeDetail({Key? key}) : super(key: key);

  @override
  State<AssetPackgeDetail> createState() => _AssetPackgeDetailState();
}

class _AssetPackgeDetailState extends State<AssetPackgeDetail> {
  late UserInfoEntity infoEntity;
  var page = 1;
  List<AssetGeneralDetailDataData> datas = [];

  @override
  void initState() {
    super.initState();
    getDatas();
  }

  getDatas() async {
    dynamic result =
        await Api.getassetDetail({'page': page.toString(), 'limit': '20', 'type': "total_assets"});
    AssetGeneralDetailEntity entity = AssetGeneralDetailEntity.fromJson(result);

    setState(() {
      if (page == 1) {
        datas.clear();
      }
      if (entity.data.data.isNotEmpty) {
        page++;
        datas.addAll(entity.data.data);
      }
    });
  }

  Color getMoneyColor(String money) {
    if (money.startsWith('-')) {
      return Colors.red;
    } else {
      return Config.colorMain;
    }
  }

  Widget getListItem(int index) {
    return Container(
      height: 124.w,
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(width: 3.w, color: Config.colorDivider)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                datas[index].memo,
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText1,
                ),
              ),
              Padding(padding: EdgeInsets.only(top: 8.w)),
              Text(
                datas[index].createdAt,
                style: TextStyle(
                  fontSize: 24.w,
                  color: Config.colorText3,
                ),
              ),
            ],
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                datas[index].money,
                style: TextStyle(
                  fontSize: 24.w,
                  color: getMoneyColor(datas[index].money),
                ),
              ),
              // Padding(padding: EdgeInsets.only(top: 8.w)),
              // Text(
              //   "已完成",
              //   style: TextStyle(
              //     fontSize: 24.w,
              //     color: Config.colorText1,
              //   ),
              // ),
            ],
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    infoEntity = Provider.of<UserInfoProvider>(context).userInfo;
    return Scaffold(
      backgroundColor: Config.colorBg2,
      appBar: AppBar(
        title: Text("总资产包"),
        centerTitle: true,
      ),
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          children: [
            Container(
              width: double.infinity,
              height: 248.w,
              padding: EdgeInsets.symmetric(vertical: 25.w, horizontal: 20.w),
              margin: EdgeInsets.fromLTRB(30.w, 30.w, 30.w, 0),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12.w),
              ),
              child: Column(
                children: [
                  Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset(
                            'assets/images/icon_mine_allasset.png',
                            width: 30.w,
                            height: 30.w,
                          ),
                          Padding(
                            padding: EdgeInsets.only(left: 6.w),
                          ),
                          Text(
                            '总资产',
                            style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                          ),
                        ],
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 8.w),
                      ),
                      Text(
                        infoEntity.data.totalAssets,
                        style: TextStyle(
                            fontSize: 28.sp, color: Config.colorMain, fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                  Divider(
                    color: Config.colorDivider,
                    height: 40.w,
                    thickness: 2.w,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      InkWell(
                        onTap: () {
                          Routes.navigateTo(context, Routes.assetGold);
                        },
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'assets/images/icon_mine_gold.png',
                                  width: 30.w,
                                  height: 30.w,
                                ),
                                Padding(
                                  padding: EdgeInsets.only(left: 6.w),
                                ),
                                Text(
                                  '资产金券',
                                  style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                                ),
                              ],
                            ),
                            Text(
                              infoEntity.data.assetVouchers,
                              style: TextStyle(fontSize: 26.sp),
                            ),
                          ],
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          Routes.navigateTo(context, Routes.assetGeneralDetail, params: {
                            'pageTitle': '购物积分',
                            'assetType': 'shopping_wallet',
                            'amount': infoEntity.data.shoppingWallet
                          });
                        },
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'assets/images/icon_mine_present_exp.png',
                                  width: 30.w,
                                  height: 30.w,
                                ),
                                Padding(
                                  padding: EdgeInsets.only(left: 6.w),
                                ),
                                Text(
                                  '购物积分',
                                  style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                                ),
                              ],
                            ),
                            Text(
                              infoEntity.data.shoppingWallet,
                              style: TextStyle(fontSize: 26.sp),
                            ),
                          ],
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          Routes.navigateTo(context, Routes.assetGeneralDetail, params: {
                            'pageTitle': '公益基金',
                            'assetType': 'fund_wallet',
                            'amount': infoEntity.data.fundWallet
                          });
                        },
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset(
                                  'assets/images/icon_mine_fund.png',
                                  width: 30.w,
                                  height: 30.w,
                                ),
                                Padding(
                                  padding: EdgeInsets.only(left: 6.w),
                                ),
                                Text(
                                  '公益基金',
                                  style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                                ),
                              ],
                            ),
                            Text(
                              infoEntity.data.fundWallet,
                              style: TextStyle(fontSize: 26.sp),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            Container(
                width: double.infinity,
                height: double.infinity,
                padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
                margin: EdgeInsets.only(top: 305.w, left: 30.w, right: 30.w, bottom: 30.w),
                decoration: BoxDecoration(
                  color: (Config.colorWhite),
                  borderRadius: BorderRadius.circular(12.w),
                ),
                child: datas.isNotEmpty
                    ? EasyRefresh(
                        header: const MaterialHeader(),
                        footer: const MaterialFooter(),
                        onRefresh: () async {
                          await Future.delayed(const Duration(milliseconds: 200), () {
                            page = 1;
                            getDatas();
                          });
                        },
                        onLoad: () async {
                          getDatas();
                        },
                        child: ListView.builder(
                          // physics: const NeverScrollableScrollPhysics(),
                          itemCount: datas.length,
                          padding: EdgeInsets.symmetric(vertical: 1.w, horizontal: 1.w),
                          itemBuilder: (BuildContext context, int index) {
                            return getListItem(index);
                          },
                        ),
                      )
                    : showNullData())
          ],
        ),
      ),
    );
  }
}
